#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int NMAX=2e5+5;
vector<int>v[NMAX];
int dist[NMAX];
int dp[NMAX];
bool viz[NMAX];
void bfs(int x)
{
queue<int>q;
dist[x]=0;
viz[x]=true;
q.push(x);
while(!q.empty())
{
int p=q.front();
q.pop();
for(auto i:v[p])
{
if(!viz[i])
{
viz[i]=true;
dist[i]=dist[p]+1;
q.push(i);
}
}
}
}
void dfs(int p)
{
viz[p]=true;
dp[p]=dist[p];
for(auto i:v[p])
{
if(!viz[i] && dist[p]<dist[i])
dfs(i);
if(dist[p]<dist[i])
dp[p]=min(dp[p],dp[i]);
else
dp[p]=min(dp[p],dist[i]);
}
}
void solve()
{
int n,m,i,j;
cin>>n>>m;
for(i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back(y);
}
bfs(1);
for(i=1;i<=n;i++)
viz[i]=false;
dfs(1);
for(i=1;i<=n;i++)
cout<<dp[i]<<" ";
cout<<"\n";
for(i=1;i<=n;i++)
{
v[i].clear();
dist[i]=0;
viz[i]=false;
dp[i]=0;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
443A - Anton and Letters | 1478B - Nezzar and Lucky Number |
228A - Is your horseshoe on the other hoof | 122A - Lucky Division |
1611C - Polycarp Recovers the Permutation | 432A - Choosing Teams |
758A - Holiday Of Equality | 1650C - Weight of the System of Nested Segments |
1097A - Gennady and a Card Game | 248A - Cupboards |
1641A - Great Sequence | 1537A - Arithmetic Array |
1370A - Maximum GCD | 149A - Business trip |
34A - Reconnaissance 2 | 59A - Word |
462B - Appleman and Card Game | 1560C - Infinity Table |
1605C - Dominant Character | 1399A - Remove Smallest |
208A - Dubstep | 1581A - CQXYM Count Permutations |
337A - Puzzles | 495A - Digital Counter |
796A - Buying A House | 67A - Partial Teacher |
116A - Tram | 1472B - Fair Division |
1281C - Cut and Paste | 141A - Amusing Joke |